﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static WinFormsApp1.Leo.ListWeeks03.SaleDisliteShow;
using WinFormsApp1.Common;
using static WinFormsApp1.Leo.ClientserShow;
using WinFormsApp1.TongShiWei.PigData;
using NPOI.SS.Formula.Functions;

namespace WinFormsApp1.Leo.ListWeeks03
{
    public partial class SaleShow : Form
    {

        public SaleShow()
        {
            InitializeComponent();
            GetCheck();
            GetUPCData();
            GetProduct();
            GetDepot();
            uiTextBox5.Text = DateTime.Now.ToString("yyyy-MM-dd");
        }

        public class Saleres
        {
            public int pageIndex { get; set; }

            public int pageSize { get; set; }
        };


        public int pageIndex = 1;

        public int pageSize = 7;

        public int totalcount = 0;

        public int pagecount = 0;

        private async void SaleShow_Load(object sender, EventArgs e)
        {
            var clients = new Saleres
            {
                pageIndex = pageIndex,
                pageSize = pageSize,
            };

            var items = await HttpClientHelper.RequestUrl<object, ApiResultFY1<List<Saless>>>(URLCommon.Url2, "/WeekListOne_Leo/GeSaleShow", HttpClientHelper.MethodType.POST, clients);
            this.dataGridView1.DataSource = items.Data.list;
            totalcount = items.Data.totalcount;
            pagecount = items.Data.pagecount;

            label1.Text = "共" + totalcount + "条,每页" + pageSize + "条，当前第" + pageIndex + "页，共" + pagecount + "页";
        }

        public class Saless
        {
            public int SaleId { get; set; }

            /// <summary>
            /// 进场编号
            /// </summary>
            public string CheckInNo { get; set; }

            /// <summary>
            /// 商品代号
            /// </summary>
            public string UPCDataName { get; set; }

            /// <summary>
            /// 件数
            /// </summary>
            public string SaleNum { get; set; }

            /// <summary>
            /// 毛重
            /// </summary>
            public string SaleGrossWeight { get; set; }

            /// <summary>
            /// 皮重
            /// </summary>
            public string SaleTares { get; set; }

            /// <summary>
            /// 净重
            /// </summary>
            public string SaleWeight { get; set; }

            /// <summary>
            /// 商品批号
            /// </summary>
            public string ProductName { get; set; }

            /// <summary>
            /// 备注
            /// </summary>
            public string SaleRemark { get; set; }

            /// <summary>
            /// 仓库
            /// </summary>
            public string DepotTypeName { get; set; }

            /// <summary>
            /// 称重时间
            /// </summary>
            public DateTime? SaleTime { get; set; }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (pageIndex != 1)
            {
                pageIndex = 1;
            }
            SaleShow_Load(sender, e);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (pageIndex > 1)
            {
                pageIndex--;
            }
            else
            {
                //弹框提示
                MessageBox.Show("已经第一页了");
            }
            SaleShow_Load(sender, e);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (pageIndex < pagecount)
            {
                pageIndex++;
            }
            else
            {
                //弹框提示
                MessageBox.Show("已经最后一页了");
            }
            SaleShow_Load(sender, e);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (pageIndex != pagecount)
            {
                pageIndex = pagecount;
            }
            SaleShow_Load(sender, e);
        }


        public record Saleste(int SaleId);
        //删除

        private async void dataGridView1_CellClick_1(object sender, DataGridViewCellEventArgs e)
        {
            if (MessageBox.Show("您确定要删除吗？", "此删除为逻辑删除", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes)
            {
                if (e.ColumnIndex == dataGridView1.Columns["删除"].Index)
                {
                    var wins = (int)e.RowIndex;
                    if (wins >= 0)
                    {
                        int SaleId = Convert.ToInt32(dataGridView1.Rows[wins].Cells["序号"].Value);

                        Saleste saless = new(SaleId);

                        var items = await HttpClientHelper.RequestUrl<Saleste, ApiResult<List<Saless>>>(URLCommon.Url1, "/WorkList01_leo/GetSaleDelete", HttpClientHelper.MethodType.POST, saless);

                        if (items.Code == 1)
                        {
                            MessageBox.Show("删除成功");
                        }
                        else
                        {
                            MessageBox.Show("删除失败");
                        }


                    }
                }
            }

        }

        //进场编号下拉
        public async void GetCheck()
        {
            CheckIn supplier = new();

            var items = await HttpClientHelper.RequestUrl<CheckIn, ApiResult<List<CheckIn>>>(URLCommon.Url2, "/CheckIn/CheckIn2Show", HttpClientHelper.MethodType.POST, supplier);

            uiComboBox1.DataSource = items.Data;
            uiComboBox1.DisplayMember = "CheckInNo";
            uiComboBox1.ValueMember = "CheckInId";
        }

        public class CheckIn
        {
            public int CheckInId { get; set; }
            public string CheckInNo { get; set; }
        }


        //商品代号
        public async void GetUPCData()
        {
            var items = await HttpClientHelper.RequestUrl<string, ApiResult<List<UPCDataser>>>(URLCommon.Url2, "/WeekListOne_Leo/GetUPCDataShow", HttpClientHelper.MethodType.GET, default);

            uiComboBox2.DataSource = items.Data;
            uiComboBox2.DisplayMember = "UPCDataName";
            uiComboBox2.ValueMember = "GoodsMinuteId";

        }

        public class UPCDataser
        {
            public int GoodsMinuteId { get; set; }
            public string UPCDataName { get; set; }
        }

        //仓库
        public async void GetDepot()
        {
            var items = await HttpClientHelper.RequestUrl<string, ApiResult<List<Depotes>>>(URLCommon.Url2, "/Tsw/GetDepottypeData", HttpClientHelper.MethodType.GET, default);

            uiComboBox3.DataSource = items.Data;
            uiComboBox3.DisplayMember = "DepotTypeName";
            uiComboBox3.ValueMember = "DepotTypeId";

        }

        public class Depotes
        {
            public int DepotTypeId { get; set; }
            public string DepotTypeName { get; set; }
        }

        //商品批号
        public async void GetProduct()
        {
            var items = await HttpClientHelper.RequestUrl<string, ApiResult<List<Productser>>>(URLCommon.Url2, "/WeekListOne_Leo/GetProductShow", HttpClientHelper.MethodType.GET, default);

            uiComboBox4.DataSource = items.Data;
            uiComboBox4.DisplayMember = "ProductName";
            uiComboBox4.ValueMember = "ProductId";
        }

        public class Productser
        {
            public int ProductId { get; set; }
            /// <summary>
            /// 商品批号名称
            /// </summary>
            public string ProductName { get; set; }
        }

        public record Salerest(int CheckInId, int MarkId, string SaleNum, string SaleGrossWeight, string SaleTares, string SaleWeight,
           int ProductId, string SaleRemark, int DepotId, DateTime SaleTime, bool IsDelete);

        //添加
        private async void uiButton20_Click(object sender, EventArgs e)
        {
            var CheckInId = Convert.ToInt32(uiComboBox1.SelectedValue);
            var MarkId = Convert.ToInt32(uiComboBox2.SelectedValue);
            var SaleNum = GoodsNum.Text;
            var SaleGrossWeight = ScaleJNum.Text;
            var SaleTares = ScalePNum.Text;
            var SaleWeight = ScaleZNum.Text;
            var ProductId = Convert.ToInt32(uiComboBox4.SelectedValue);
            var SaleRemark = uiTextBox17.Text;
            var DepotId = Convert.ToInt32(uiComboBox3.SelectedValue);
            var SaleTime = Convert.ToDateTime(uiDatePicker1.Text);
            var IsDelete = false;



            Salerest clients = new(CheckInId, MarkId, SaleNum, SaleGrossWeight, SaleTares, SaleWeight, ProductId, SaleRemark, DepotId, SaleTime, IsDelete);
            var res = await HttpClientHelper.RequestUrl<Salerest, ApiResult<Saleretreas>>(URLCommon.Url1, "/WorkList01_leo/GetSaleAdd", HttpClientHelper.MethodType.POST, clients);
            if (res.Code == 1)
            {
                MessageBox.Show("添加成功");
            }
        }

        public class Saleretreas
        {
            public int SaleId { get; set; }

            /// <summary>
            /// 进场编号
            /// </summary>
            public int CheckInId { get; set; }

            /// <summary>
            /// 商品代号
            /// </summary>
            public int MarkId { get; set; }

            /// <summary>
            /// 件数
            /// </summary>
            public string SaleNum { get; set; }

            /// <summary>
            /// 毛重
            /// </summary>
            public string SaleGrossWeight { get; set; }

            /// <summary>
            /// 皮重
            /// </summary>
            public string SaleTares { get; set; }

            /// <summary>
            /// 净重
            /// </summary>
            public string SaleWeight { get; set; }

            /// <summary>
            /// 商品批号
            /// </summary>
            public int ProductId { get; set; }

            /// <summary>
            /// 备注
            /// </summary>
            public string SaleRemark { get; set; }

            /// <summary>
            /// 仓库
            /// </summary>
            public int DepotId { get; set; }

            /// <summary>
            /// 称重时间
            /// </summary>
            public DateTime? SaleTime { get; set; }

            /// <summary>
            /// 是否删除
            /// </summary>
            public bool IsDelete { get; set; } = false;
        }
    }
}
